<template>
    <div v-loading="upLoading">
        <el-row
            :gutter="10"
            class="mb8 clearfix "
            style="margin-top:2px;">
                    <el-col :span="18">
                        <el-form
                            :model="queryParams"
                            ref="queryForm"
                            v-show="showSearch"
                            :inline="true"
                        >
                            <el-form-item
                                label="名称"
                                prop="zoneName"
                            >
                                <el-input
                                    v-model="queryParams.zoneName"
                                    placeholder="请输入名称"
                                    clearable
                                    size="small"
                                    style="width: 240px"
                                />
                            </el-form-item>
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    icon="el-icon-search"
                                    size="mini"
                                    @click="handleQuery"
                                >搜索</el-button>
                                <el-button
                                    icon="el-icon-refresh"
                                    size="mini"
                                    @click="resetQuery"
                                >重置</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col
                        :span="1.5"
                        style="float:right;margin:1px 5px 0px 0px;"
                    >
                    <el-upload
                        class="upload-xlsx"
                        :action="'/api'+uploadUrl"
                        :show-file-list="false"
                        accept=".xlsx"
                        :before-upload="onChangeFile"
                        :on-success="handleXlsx"
                        :data="formDataXlsx"
                        >
                        <el-button size="small" type="primary">文件导入</el-button>
                    </el-upload>
                    <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="small"
                        @click="handleAddZone($event)"                         
                    >添加</el-button>
                    </el-col>
        </el-row>
        <div
            class="table"
            style="height:100%;background:white;padding:20px"
        >
            <el-table
                v-loading="loading"
                :data="dataList"
                @selection-change="handleSelectionChange"
                class="zoneTask"
                :header-cell-style="{background:'#DADDE9',color:'#000',height:'44px'}"
            >
                <el-table-column
                    type="selection"
                    width="80"
                    align="center" 
                />
                <el-table-column
                    label="名称"
                    prop="zoneName"
                    :show-overflow-tooltip="true"
                    width="150"
                />
                <el-table-column
                    label="负责人"
                    prop="zoneName"
                    :show-overflow-tooltip="true"
                    width="150"
                >
                <template slot-scope="scope">
                        {{getUserByuserId(scope.row.userId)}} 
                </template>
                </el-table-column>
                <el-table-column
                    label="状态"
                    prop="progressStatus"
                    align="center"
                >
                <template slot-scope="scope">
                    <div v-if="scope.row.zoneTasks==undefined">
                            <span :class="statusDeal(scope.row.progressStatus).style" class="statusSpan" >{{ statusDeal(scope.row.progressStatus).text }}</span>  
                    </div>
                </template>
                </el-table-column>
                <el-table-column
                    label="创建时间"
                    prop="createTime"
                    align="center"
                    width="250"
                >
                    <template slot-scope="scope">
                        <span>{{ dealTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding"
                >
                    <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="text"
                                @click="handleUpdateZone(scope.row)"
                            >查看</el-button>
                            <el-button
                                size="mini"
                                type="text"
                                
                                @click="handleDelete(scope.row)"
                            >删除</el-button>
                        
                    </template>
                </el-table-column>
            </el-table>
            <el-row class="clearfix">
                <el-button
                    class="delete"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                >删除</el-button>
                <pagination
                    v-show="total>0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getZoneList"
                />
            </el-row>
        </div>
    </div>
</template>

<script>
import { listZone,delZone} from "@/api/project/project";
import { getToken } from "@/utils/auth";
export default {
    name: 'proZone',
    props:{
        formData:{},
        //负责人选项
        userOptions:{
            type:Array
        }
    },
    data() {
        return {
            //上传地址
            uploadUrl:"/v1/AnalysisInfo/SoilController/GetExcelResult",
            //上传请求头
            headers:{'GeomativeAuthorization':'Geomative ' + getToken()},
            //上传参数
            formDataXlsx:{
                projectId:null
            },
            // 遮罩层
            loading: false,
            upLoading:false, 
            //场地列表
            dataList:[],
            //项目列表请求参数
            queryParams:{
                pageNum: 1,
                pageSize: 10,
                zoneName: undefined
            },
            // 显示搜索条件
            showSearch: true,
            //是否显示场地弹出层
            openZone:false,
             // 总条数
            total: 0,
            // 非多个禁用
            multiple: true,
            zoneRules:{
                zoneName: [
                    { required: true, message: "场地名称不能为空", trigger: "blur" }
                ],
              
                methodDesignId: [
                    { required: true, message: "设计方法不能为空", trigger: "blur" }
                ],
            },
            taskRules:{    
            },
            // 指派任务弹出层
            openTask:false,
            taskTitle:"",
            defaultProps: {
                children: "children",
                label: "label"
            },
            //分区表单参数
            zoneForm:{},
            //任务表单参数
            taskForm:{},
             //分区弹出层
            zoneTitle:"",
            //方法设计选择
            taskOptions:this.$store.getters.taskOptions,
            // 机构下拉
            agencyOptions:[],
        };
    },
    mounted() {
    },
    watch:{
        formData(val){  
            this.formDataXlsx.projectId = val.projectId
            this.getZoneList();
        }
    },
    methods: {
        //获取分区列表
        getZoneList() {
            this.loading = true;
            const queryId = this.formData.projectId;
            this.queryParams.projectId = queryId;
            listZone(this.queryParams).then(
                response => {
                    this.dataList = response.data.records;
                    this.total = response.data.total;
                    this.loading = false;
                }
            );
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getZoneList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item['zoneId'])
            this.multiple = !selection.length
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            if(!this.$store.getters.permissions.includes("*:*:*")&&!this.$store.getters.permissions.includes('project:zone:delete')){
                this.Message({
                    message:"无权限!",
                    type:'error'
                })
                return;
            }
            let zoneIds = undefined;
            if (row.zoneId == null) {
                zoneIds = this.ids
            } else {
                zoneIds = [row.zoneId]
            }
            // console.log(this.ids)
            this.$confirm(`是否确认删除此 ${zoneIds.length} 条场地据项?`, "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delZone(zoneIds);
            }).then(() => {
                this.getZoneList();
                this.msgSuccess("删除成功");
            }).catch(() => { })
        },
        //重置分区表单
        restZone(){
             this.zoneForm = {
                projectId:this.formData.projectId,
                projectName:this.formData.projectName,
                zoneId:undefined,
                zoneName:undefined,
                address:undefined,
                methodDesignId:undefined,
                methodDesign:undefined,
                tasks:[],
                tasksName:undefined,
                remark:undefined,
             }
            this.resetForm("zoneForm");
        },
        // 新增分区按钮操作
        handleAddZone(){
            if(this.formData.projectId!=undefined){
                this.$router.push({ name:'AddZone/:ID',params:{ID:this.formData.projectId} });
            }
            else{
                this.Message({
                    message:"必须选择项目",
                    type:'error'
                })
            }
            
        },
        //移除参数
        handleClose(id){
            this.form.zoneNames.splice(id,1)
        },
        // 取消按钮
        cancel() {
            this.openZone = false;
            this.openTask = false;
            this.reset();
        },
     
        /** 分区修改按钮操作 */
        handleUpdateZone(zoneRow) {
                this.$router.push({ name:'UpdateZone/:ID',params:{ID:zoneRow.zoneId} });
        },
        //获取负责人
        getUserByuserId(userId){
            let userName = this.userOptions.filter(item=>{
                return item.id == userId
            })
            if(!userName[0]){
                return ""
            }
           return userName[0].label
        },
        //状态样式0未开始，1是已开始，2是已完成，3是已作废
        statusDeal(val){
            if(val=="0"){
                return {'style':"bgst0",'text':"未开始"}
            }
            else if(val=="1"){
                return {'style':"bgst1",'text':"已开始"}
            }
            else if(val=="2"){
                return {'style':"bgst2",'text':"已完成"}
            }
            else{
                return {'style':"bgst3",'text':"已作废"}
            }
        },
        //文件上传成功
        handleXlsx(response){
            if(response.code!=200){
                this.msgError("导入失败,"+response.msg);
            }
            else{
                this.msgSuccess("导入成功");
                this.dataTypeList = response.result
                this.getZoneList()
            }
            this.upLoading = false
        },
        //文件改变
        onChangeFile(){
            this.upLoading = true 
        }
    },
};
</script>

<style lang="scss" scoped>
.delete {
    margin-top: 25px;
    margin-left: 1px;
}
.statusSpan{
    color: white;
    font-size: 10px !important;
    padding: 2px;
    border-radius: 2px;
}
.bgst0{  
    background:#307AFF ;
}
.bgst1{  
    background:rgba(255, 125, 41, 1) ;
}
.bgst2{  
    background:#62D566 ;
}
.bgst3{  
    background:#A6A6A6 ;
}
.upload-xlsx{
    display: inline-block;
    margin-right: 20px;
}
</style>